package com.zhangdi.dwdata.query;

import javax.persistence.Query;
import javax.persistence.TypedQuery;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangdi on 2020/09/16 12:00:04.
 */
public class QFinder {
    public static QFinder create() {
        return new QFinder();
    }

    public static QFinder create(Class clazz) {
        return new QFinder(clazz);
    }

    public QFinder(){

    }

    public QFinder(Class clazz){
        createQuery(clazz);
    }

    public StringBuilder createQuery(Class clazz) {
        hqlBuilder.append(FROM);
        hqlBuilder.append(clazz.getSimpleName());
        hqlBuilder.append(EMPTY);
        hqlBuilder.append(ALISA);
        hqlBuilder.append(EMPTY);
        return hqlBuilder;
    }

    private StringBuilder hqlBuilder = new StringBuilder();
    private List<String> params = new ArrayList<>();
    private List<Object> values = new ArrayList<>();

    private List<Filter> filters = new ArrayList<>();
    private List<Order> orders = new ArrayList<>();

    public String getHql() {
        return hqlBuilder.toString();
    }

    public List<String> getParams() {
        return params;
    }

    public void setParams(List<String> params) {
        this.params = params;
    }

    public List<Object> getValues() {
        return values;
    }

    public void setValues(List<Object> values) {
        this.values = values;
    }

    public List<Filter> getFilters() {
        return filters;
    }

    public void setFilters(List<Filter> filters) {
        this.filters = filters;
    }

    public List<Order> getOrders() {
        return orders;
    }

    public void setOrders(List<Order> orders) {
        this.orders = orders;
    }


    public static final String ROW_COUNT = " select count(*) ";
    public static final String FROM = " from ";
    public static final String DISTINCT = " distinct ";
    public static final String HQL_FETCH = " fetch ";
    public static final String ORDER_BY = " order ";
    private static final String EMPTY = " ";
    private static final String ALISA = "a";

    public QFinder append(String hql) {
        hqlBuilder.append(EMPTY);
        hqlBuilder.append(hql);
        hqlBuilder.append(EMPTY);
        return this;
    }

    public void addParam(String param, Object val) {
        this.params.add(param);
        this.values.add(val);
    }

    /**
     * 获得查询数据库记录数的hql语句。
     *
     * @return
     */
    public String getRowCountHql() {
        String hql = hqlBuilder.toString();

        int fromIndex = hql.toLowerCase().indexOf(FROM);
        String projectionHql = hql.substring(0, fromIndex);

        hql = hql.substring(fromIndex);
        String rowCountHql = hql.replace(HQL_FETCH, "");

        int index = rowCountHql.indexOf(ORDER_BY);
        if (index > 0) {
            rowCountHql = rowCountHql.substring(0, index);
        }
        return wrapProjection(projectionHql) + rowCountHql;
    }

    private String wrapProjection(String projection) {
//		if (projection.indexOf("select") == -1) {
//			return ROW_COUNT;
//		} else {
//			return projection.replace("select", "select count(") + ") ";
//		}
        return ROW_COUNT;
    }

    public void setQueryParams(TypedQuery query) {
        for (int i = 0, length = params.size(); i < length; i++) {
            query.setParameter(params.get(i), values.get(i));
        }
    }

    public void setQueryParams(Query query) {
        for (int i = 0, length = params.size(); i < length; i++) {
            query.setParameter(params.get(i), values.get(i));
        }
    }
}
